#!/usr/bin/env zsh

# Define colors
orange="%F{166}"
purple="%F{135}"
hotpink="%F{161}"
limegreen="%F{118}"

# Set required options
#
setopt prompt_subst

# Load required modules
#
autoload -Uz vcs_info

# Set vcs_info parameters
#
zstyle ':vcs_info:*' enable hg bzr git
zstyle ':vcs_info:*:*' unstagedstr '!'
zstyle ':vcs_info:*:*' stagedstr '+'
zstyle ':vcs_info:*:*' formats "[%r]" "%s/%b" "%%u%c"
zstyle ':vcs_info:*:*' actionformats "[%r]" "%s/%b" "%u%c (%a)"
zstyle ':vcs_info:*:*' nvcsformats "" "" ""

# Display information about the current directory
#
dir_info() {
    echo "%F{magenta}${PWD/#$HOME/~}%f "
}

# Git short sha
#
local git_sha='$(git_prompt_short_sha)'
ZSH_THEME_GIT_PROMPT_SHA_BEFORE="%{$orange%}("
ZSH_THEME_GIT_PROMPT_SHA_AFTER=")%f "

# Fastest possible way to check if repo is dirty
#
git_dirty() {
    # Check if we're in a git repo
    command git rev-parse --is-inside-work-tree &>/dev/null || return
    # Check if it's dirty
    command git diff --quiet --ignore-submodules HEAD &>/dev/null; [ $? -eq 1 ] && echo "*"
}

# Display information about the current repository
#
repo_information() {
    if [ -z $vcs_info_msg_0_ ]; then
        echo "%F{cyan}$vcs_info_msg_0_%f"
    else
	echo "%F{cyan}${vcs_info_msg_0_%%/.} %F{8}$vcs_info_msg_1_`git_dirty` $vcs_info_msg_2_%f"
    fi
}

precmd() {
    vcs_info # Get version control info before we start outputting stuff
    print -P "\n%F{blue}#%f $(dir_info)$(repo_information)${git_sha}%F{yellow}[%*]%f"
}

# Define prompts
#
PROMPT="%(?.%F{green}.%F{red})❯%f " # Display a red prompt char on failure
RPROMPT="%F{8}${SSH_TTY:+%n@%m}%f"    # Display username if connected via SSH

# ------------------------------------------------------------------------------
#
# List of vcs_info format strings:
#
# %b => current branch
# %a => current action (rebase/merge)
# %s => current version control system
# %r => name of the root directory of the repository
# %S => current path relative to the repository root directory
# %m => in case of Git, show information about stashes
# %u => show unstaged changes in the repository
# %c => show staged changes in the repository
#
# List of prompt format strings:
#
# prompt:
# %F => color dict
# %f => reset color
# %~ => current path
# %* => time
# %n => username
# %m => shortname host
# %(?..) => prompt conditional - %(condition.true.false)
#
# $FX[bold] => bold font
# $FX[no-bold] => no-bold font
# ------------------------------------------------------------------------------
#
